/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.brands.application.ics.delegate;

import br.com.brands.application.ics.beans.CategoryBeans;
import br.com.brands.application.ics.factory.DAOFactory;
import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

/**
 *
 * @author ppacheco
 */
public class CategoryDelegate implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -4956057345441739631L;
    private static CategoryDelegate instance = null;
    private static final Logger log = Logger.getLogger(CategoryDelegate.class.getName());

    public CategoryDelegate() {
        log.log(Level.INFO, "Loading CategoryDelegate.");
    }

    public CategoryBeans save(CategoryBeans category) throws HibernateException, InstantiationException {

        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (category != null) {
                DAOFactory.getInstance().getCategoryDAOImpl().save(category);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return category;
    }

    public CategoryBeans saveOrUpdate(CategoryBeans category) throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (category != null) {
                DAOFactory.getInstance().getCategoryDAOImpl().saveOrUpdate(category);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return category;
    }

    public CategoryBeans delete(CategoryBeans category) throws HibernateException, InstantiationException {
        try {
            log.log(Level.INFO, "Executing at Delegate layer level");
            if (category != null) {
                DAOFactory.getInstance().getCategoryDAOImpl().delete(category);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return category;
    }

    public CategoryBeans findByID(CategoryBeans category, Long id, boolean lock) throws HibernateException, InstantiationException {

        CategoryBeans objRef = null;

        try {
            if (id != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                objRef = (CategoryBeans) DAOFactory.getInstance().getCategoryDAOImpl().findByID(category, id, lock);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return objRef;
    }

    @SuppressWarnings("unchecked")
    public List<CategoryBeans> findAll(CategoryBeans category) throws HibernateException, InstantiationException {

        List<CategoryBeans> list = null;

        try {
            if (category != null) {
                log.log(Level.INFO, "Executing at Delegate layer level");
                list = DAOFactory.getInstance().getCategoryDAOImpl().findAll(category);
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return list;
    }

    public static CategoryDelegate getInstance() throws InstantiationException {
        try {
            if (instance == null) {
                instance = new CategoryDelegate();
            }
        } catch (Exception e) {
            log.log(Level.ERROR, e.getMessage(), e);
        }
        return instance;
    }
}
